package com.piggy.settlement.sdk.client.sign;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.PiggySignConstants;
import com.piggy.settlement.sdk.util.PiggyVerifyUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：daxia
 * @description：电签相关接口
 * @modified By：
 * @version: 1.0$
 */
public class PiggySignApiClient {

    static PiggySignApiClient piggySignApiClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggySignApiClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggySignApiClient getInstanse(String appSecret, String domain) {
        if (piggySignApiClient == null) {
            piggySignApiClient = new PiggySignApiClient(appSecret, domain);
        }

        return piggySignApiClient;
    }

    /**
     * 根据用户信息获取签约页面html
     *
     * @param signVo 参数实体
     * @return
     */
    public String getSignPage(Object signVo)  throws Exception{

        String response = "";
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signVo);
            response =
                    HttpRequest.post(domain + PiggySignConstants.HAS_KEY)
                            .header("sig", signStr)
                            .form(BeanUtil.beanToMap(signVo))
                            .timeout(20000)
                            .execute()
                            .body();
            return response;

    }

    /**
     * 根据用户信息获取签约页面url
     *
     * @param signVo 参数实体
     * @return
     */
    public JSONObject getSignUrl(Object signVo) throws Exception{

        String response = "";
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signVo);
            response =
                    HttpRequest.post(domain + PiggySignConstants.HAS_KEY_BY_URL)
                            .header("sig", signStr)
                            .form(BeanUtil.beanToMap(signVo))
                            .timeout(20000)
                            .execute()
                            .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;

    }

    /**
     * 根据用户信息获取签约结果
     *
     * @param signVo 参数实体
     * @return
     */
    public JSONObject getSignResult(Object signVo) throws Exception {

        String response = "";
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signVo);
            response =
                    HttpRequest.post(domain + PiggySignConstants.GET_SIGNED_RESULT)
                            .header("sig", signStr)
                            .form(BeanUtil.beanToMap(signVo))
                            .timeout(20000)
                            .execute()
                            .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;

    }

    /**
     * 根据用户信息获取签约状态
     *
     * @param signVo 参数实体
     * @return
     */
    public JSONObject getSignStatus(Object signVo)throws Exception   {

        String response = "";
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signVo);
            response =
                    HttpRequest.post(domain + PiggySignConstants.GET_SIGN_STATUS)
                            .header("sig", signStr)
                            .form(BeanUtil.beanToMap(signVo))
                            .timeout(20000)
                            .execute()
                            .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;

    }

    /**
     * 根据合同编号获取在线预览地址
     *
     * @param documentId 合同编号
     * @return
     */
    public JSONObject viewContract(String documentId) throws Exception  {

        String response = "";
            Map<String, Object> map = new HashMap<>();
            map.put("documentId", documentId);
            response = HttpUtil.get(domain + PiggySignConstants.VIEW_CONTRACT, map);

            JSONObject object = JSONUtil.parseObj(response);
            return object;

    }
}
